public class MyLinkedList implements IList{

    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    @Override
        //头插法插入节点
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = last = node;
        }else{
            node.prev = last;
            last.next = node;
            last = node;
        }
    }

    @Override
    public void addIndex(int data, int index) {
        int len = size();
        if(index<0 || index>len){
            return;
        } else if(index == 0){
            addFirst(data);
            return;
        } else if(index == len){
            addLast(data);
            return;
        } else{
            ListNode cur = head;
            ListNode node = new ListNode(data);
            int cont = index;
            while(cont!=1){
                cont--;
                cur=cur.next;
            }
            node.prev = cur;
            node.next = cur.next;
            cur.next.prev = node;
            cur.next = node;
            return;
        }

    }

    @Override
    public boolean contain(int key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        /*if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        boolean b = false;
        ListNode cur = head;
        ListNode prev = null;
        while(cur!=null){
            if(cur.val == key){
                b = true;
                break;
            }
            prev = cur;
            cur = cur.next;
        }
        if( b = true){
            prev.next = cur.next;
            cur.next.prev = cur.prev;
            return;
        }else{
            return;
        }*/
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        last = last.prev;
                    }else {
                        cur.next = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAll(int key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        last = last.prev;
                    }else {
                        cur.next = cur.prev;
                    }
                }

            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int cont = 0;
        while(cur != null){
            cont++;
            cur = cur.next;
        }
        return cont;
    }

    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public boolean contains(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //链表完结
    //
}
